package com.rgs.gateway.web.common.design.sign.strategy;

import com.alibaba.fastjson.JSON;
import com.rgs.gateway.common.enums.SignTypeEnum;
import com.rgs.gateway.common.exception.ServiceException;
import com.rgs.gateway.web.utils.GatewaySignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.util.Map;

/**
 * @author RenGaoshuai
 * @date 2024/7/1 16:45
 * @description
 */
@Slf4j
public class CommonGatewaySignStrategy implements GatewaySignStrategy {

    @Override
    public String getSign(String appKey, String appSecret, String timestamp, Map<String, Object> params) {
        try {
            // 参数进行字典排序
            String ret = JSON.toJSONString(GatewaySignUtils.sortJson(params));
            //转化位MD5
            return DigestUtils.md5DigestAsHex(GatewaySignUtils.concatenate(ret.getBytes(), appSecret.getBytes()));
        } catch (Exception e) {
            log.error("通用验签生成数据异常,异常信息为：{}", e.getMessage());
            throw new ServiceException("通用验签生成数据异常");
        }
    }

    @Override
    public Integer getSignType() {
        return SignTypeEnum.COMMON.getCodeValue();
    }
}
